import java.util.LinkedList;
import java.util.Queue;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User: Administrator
 * Data: 2023 - 08 - 21
 * Time: 11:47
 */
//力扣225.用队列实现栈
//法一：用一个队列
class MyStack {
    Queue<Integer> queue;

    public MyStack() {
        queue = new LinkedList<>();
    }

    public void push(int x) {
            queue.offer(x);
    }

    public int pop() {
        int size = queue.size() - 1;
        for (int i = 0; i < size; i++) {//这里用了出时将队列处理成栈的形式，也可以在进时将队列处理成栈的形式
            int x = queue.poll();
            queue.offer(x);
        }
        return queue.poll();
    }

    public int top() {
        int x = 0;
        int size = queue.size();
        for (int i = 0; i < size; i++) {
            x = queue.poll();
            queue.offer(x);
        }
        return x;
    }

    public boolean empty() {
        return queue.isEmpty();
    }
}




//法二：用二个队列
class MyStack1 {
    Queue<Integer> queue1;
    Queue<Integer> queue2;

    public MyStack1() {
        queue1 = new LinkedList<>();
        queue2 = new LinkedList<>();
    }

    public void push(int x) {//这里用了进时将队列处理成栈的形式，也可以在出时将队列处理成栈的形式
        if(queue1.isEmpty() && queue2.isEmpty()) {
            queue1.offer(x);
        } else if (!queue1.isEmpty() && queue2.isEmpty()) {
            queue2.offer(x);
            int size = queue1.size();
            for (int i = 0; i < size; i++) {
                int n = queue1.poll();
                queue2.offer(n);
            }
        } else if (queue1.isEmpty() && !queue2.isEmpty()){
            queue1.offer(x);
            int size = queue2.size();
            for (int i = 0; i < size; i++) {
                int n = queue2.poll();
                queue1.offer(n);
            }
        }
    }

    public int pop() {
        if(!queue1.isEmpty() && queue2.isEmpty()) {
            return queue1.poll();
        }
        return queue2.poll();
    }

    public int top() {
        if(!queue1.isEmpty() && queue2.isEmpty()) {
            return queue1.peek();
        }
        return queue2.peek();
    }

    public boolean empty() {
        return queue1.isEmpty() && queue2.isEmpty();
    }
}

